package com.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Flower;
import com.example.demo.entity.Group;
import com.example.demo.entity.User;
import com.example.demo.service.FlowerService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/flower")
public class FlowerController {
    @Resource
    FlowerService flowerService;

    @PostMapping
    public Result<?> save(@RequestBody Flower flower) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
        Date date = new Date();
        flower.setCreatedTime(formatter.format(date));
        flower.setUpdatedTime(formatter.format(date));
        flowerService.insert(flower);
        return Result.success();
    }


    //修改
    @PutMapping
    public Result<?> update(@RequestBody Flower flower) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
        Date date = new Date();
        flower.setUpdatedTime(formatter.format(date));
        flowerService.updateById(flower);
        return Result.success();
    }

    //删除
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        flowerService.deleteById(id);
        return Result.success();
    }

    //分页查询
    @GetMapping("/page")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "100000") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {

        Page<Flower> userPage = flowerService.selectPage(pageNum, pageSize, search);
        return Result.success(userPage);
    }

    @GetMapping("/page/group")
    public Result<?> findGroupPage(@RequestParam(defaultValue = "") String groupId) {
        Object userPage = "";
        if (groupId.equals("")) {
            userPage = flowerService.selectGroupPage1();
        } else {
            userPage = flowerService.selectGroupPage(groupId);
        }
        return Result.success(userPage);
    }

    @GetMapping("/search")
    public Result<?> findSearch(@RequestParam(defaultValue = "") String search) {
        List<Flower> flowerList = flowerService.findSearch(search);
        return Result.success(flowerList);
    }

    @GetMapping("/selectAll")
    public Result<?> selectAll(Flower flower) {
        List<Flower> groupList = flowerService.selectAll(flower);
        return Result.success(groupList);
    }

    @GetMapping("/selectUrl")
    public Result<?> selectUrl(Flower flower) {
        List<Flower> groupList = flowerService.selectUrl(flower);
        return Result.success(groupList);
    }

    @GetMapping("/flowerId")
    public Result<?> getFlowerId(String flowerId) {

        return Result.success(flowerService.getFlowerId(flowerId));
    }
}
